 package CurtainDesign;

 import com.sun.jimi.core.Jimi;
 import com.sun.jimi.core.component.JimiCanvasLW;
 import java.awt.BorderLayout;
 import java.awt.Font;
 import java.awt.GridLayout;
 import java.awt.Image;
 import java.awt.MediaTracker;
 import java.awt.SystemColor;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.io.File;
 import javax.swing.ButtonGroup;
 import javax.swing.JPanel;
 import javax.swing.JRadioButton;

 public class SelectStyleView extends JPanel
 {
   protected JPanel[] m_Panels;
   protected JRadioButton[] m_Radios;
   protected ButtonGroup m_RdGroup;
   protected JimiCanvasLW[] m_Canvas;
   protected MediaTracker m_Tracker;
   protected Image m_Image;
   protected String[] m_FileNames;
   protected boolean m_IsStyleChanged = false;

   public SelectStyleView() {
     try {
       jbInit();
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   }

   private void jbInit() throws Exception {
     setName(Constants.TODO[0]);

     setLayout(new GridLayout(2, 2, 20, 20));

     int number = CountPic(Constants.PIC_PATH);

     this.m_Canvas = new JimiCanvasLW[number];
     this.m_Panels = new JPanel[number];
     this.m_Radios = new JRadioButton[number];
     this.m_RdGroup = new ButtonGroup();

     for (int i = 0; i < number; i++) {
       this.m_Panels[i] = new JPanel(new BorderLayout());
       this.m_Radios[i] = new JRadioButton(this.m_FileNames[i]);
       this.m_Radios[i].setFont(new Font("Serif", 0, 13));
       this.m_Radios[i].addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
           JRadioButton b = (JRadioButton)e.getSource();
           if (b.isSelected()) {
             CurtainDesign.m_UserMainGUI.EnableNewBt();

             String shortname = b.getText().substring(0, b.getText().lastIndexOf("."));

             if ((!shortname.equals(Constants.STYLES[0])) && (!shortname.equals(Constants.STYLES[1])) && (!shortname.equals(Constants.STYLES[2])))
             {
               CurtainDesign.m_UserMainGUI.DisableNewBt();
               CurtainDesign.m_UserMainGUI.DisableOpenBt();
               CurtainDesign.m_UserMainGUI.DisableSaveBt();
               CurtainDesign.m_UserMainGUI.DisableSaveAsBt();
               CurtainDesign.m_UserMainGUI.DisableCalcBt();
             }
             if (!CurtainDesign.m_UserMainGUI.GetSelectStyle().equals(shortname))
             {
               SelectStyleView.this.m_IsStyleChanged = true;
             }
             else SelectStyleView.this.m_IsStyleChanged = false;

             CurtainDesign.m_UserMainGUI.SetSelectStyle(shortname);
           }
         }
       });
       add(this.m_Panels[i]);
       this.m_RdGroup.add(this.m_Radios[i]);
     }

     for (int i = 0; i < number; i++)
     {
       this.m_Canvas[i] = new JimiCanvasLW();
       this.m_Canvas[i].setBackground(SystemColor.LIGHT_GRAY);
       this.m_Canvas[i].setForeground(SystemColor.pink);
       this.m_Canvas[i].setWillSizeToFit(true);
       this.m_Canvas[i].setResizePolicy(0);
       this.m_Canvas[i].setJustificationPolicy(8);

       String path = Constants.PIC_PATH + File.separator + this.m_FileNames[i];
       LoadImage(path, i);

       this.m_Panels[i].add(this.m_Canvas[i], "Center");
       this.m_Panels[i].add(this.m_Radios[i], "South");

       this.m_Canvas[i].setImage(this.m_Image);
     }
   }

   private boolean LoadImage(String filepath, int index)
   {
     this.m_Image = Jimi.getImage(filepath);
     this.m_Tracker = new MediaTracker(this);
     this.m_Tracker.addImage(this.m_Image, index);
     try {
       this.m_Tracker.waitForAll();
     }
     catch (InterruptedException e) {
       this.m_Tracker.removeImage(this.m_Image);
       return false;
     }
     return true;
   }

   private int CountPic(String directory) {
     int number = 0;
     File Dir = new File(directory);
     if (!Dir.isDirectory()) {
       return -1;
     }
     String[] filelist = Dir.list();

     for (int i = filelist.length - 1; i >= 0; i--) {
       File tmpFile = new File(Dir.getAbsolutePath() + File.separator + filelist[i]);
       if ((!tmpFile.isFile()) || ((!tmpFile.getName().endsWith("jpg")) && (!tmpFile.getName().endsWith("gif")) && (!tmpFile.getName().endsWith("bmp")) && (!tmpFile.getName().endsWith("png"))))
       {
         continue;
       }
       number++;
     }

     this.m_FileNames = new String[number];

     number = 0;
     for (int i = filelist.length - 1; i >= 0; i--) {
       File tmpFile = new File(Dir.getAbsolutePath() + File.separator + filelist[i]);
       if ((!tmpFile.isFile()) || ((!tmpFile.getName().endsWith("jpg")) && (!tmpFile.getName().endsWith("gif")) && (!tmpFile.getName().endsWith("bmp")) && (!tmpFile.getName().endsWith("png"))))
       {
         continue;
       }
       this.m_FileNames[number] = new String(tmpFile.getName());
       number++;
     }

     return number;
   }

   public boolean IsStyleChanged() {
     return this.m_IsStyleChanged;
   }

   public void SetStyleChanged(boolean flag) {
     this.m_IsStyleChanged = flag;
   }

   public void SetRadioBtSelected(String style)
   {
     int number = CountPic(Constants.PIC_PATH);
     for (int i = 0; i < number; i++) {
       String name = this.m_Radios[i].getText();
       if (style.equals(name.substring(0, name.lastIndexOf("."))))
       {
         this.m_Radios[i].setSelected(true);
       }
       else this.m_Radios[i].setSelected(false);
     }
   }
 }

/* Location:           C:\Users\Administrator\Desktop\奥特百叶窗\Shutter DesignerV3.1\CurtainDesign.jar
 * Qualified Name:     CurtainDesign.SelectStyleView
 * JD-Core Version:    0.6.0
 */